﻿var reg1 = new RegExp("【", "g");
var reg2 = new RegExp("】", "g");
//获得URL参数地址
Request = {
    QueryString: function (item) {
        var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
        return svalue ? svalue[1] : svalue;
    }
}
//读取题目
var jd;//题目
function loadQuestion() {
	$.getJSON(Examconfig.apiurl,{exam:Examconfig.exam,examtype:Examconfig.examtype,infotype:Examconfig.infotype,id:Examconfig.id},function(result){
		 if(result.status==0){
			 layer.msg(result.info);
		     return;
		 }
		 document.title=result.data.title; 
		 $("#toptitle").html(result.data.title);
		 jd=result.data;
		 disp();
	 });
}
var countLine = 0;//总题数
var curLine = 0;//当前题目
var jdAct = [];             
function disp() {
    countLine = jd["count"];
    var spcountLine = document.getElementById("spcountLine");//显示题目总和
    spcountLine.innerHTML = countLine;
    jdAct = [];
    for (var i = 0; i < countLine; i++) {
    	var type = jd["rec"][i]["type"];//获取题目类型
        var AnswerTrue = jd["rec"][i]["answertruen"];//正确答案
        var jdActItem = { "type":type,"AnswerTrue": AnswerTrue, "ActAn": "", "result": "" };
        jdAct.push(jdActItem);
    }
   showPanel();//显示答题板
   if (countLine > 0) {
       showQuestion();
   }
   else {
	  layer.msg("考题数为0");
      return;
   }
}
//显示当前题
function showQuestion() {
    showPanel();//显示选择项
    panelHide();//面板隐藏
    var jdr = jd["rec"][curLine];
    var line = jdr["line"];
    var id = jdr["id"];
    var type = jdr["type"];
    var fileurl=jd["fileurl"];
    var typestr;
    switch (type)
    {
        case "1":
            typestr = "判断题";
            break;
        case "2":
            typestr = "单选题";
            break;
        case "3":
            typestr = "多选题";
            break;
        default:
            typestr = "不明";
            break;
    }
	var Question = jdr["question"];//问题
	var sinaimg = jdr["img"];//题目图片
	if (sinaimg.length > 3){
		sinaimg=contentPath+jdr["img"];
	}
	var explain = jdr["helptip"];//帮助信息
	var jq_mp3 = contentPath+jdr['mp3'];//视频地址
	var jqimg=jdr["jqimg"];
	var jqimgpath="";
	if (jqimg.length > 3){
		jqimgpath=contentPath+jqimg;
	}
	var voiceex = jdr["jqdetail"];//答题技巧
	var jqhelptip = jdr["helptip"];//解析
	var An1 = jdr["an1"];
    var An2 = jdr["an2"];
    var An3 = jdr["an3"];
    var An4 = jdr["an4"];
	var spcurLine = document.getElementById("spcurLine");
    spcurLine.innerHTML = curLine + 1;
	var divQuestion = document.getElementById("divQuestion");
    Question = replacekh(Question);
    divQuestion.innerHTML = (curLine + 1) + ". " + "<span class='radio'>" + typestr + "</span>" + Question;
    var divAn1 = document.getElementById("divAn1");
    var divAn2 = document.getElementById("divAn2");
    var divAn3 = document.getElementById("divAn3");
    var divAn4 = document.getElementById("divAn4");
    //清除样式，下面没有注释的才是对的
    divAn1.removeAttribute("style");
    divAn2.removeAttribute("style");
    divAn3.removeAttribute("style");
    divAn4.removeAttribute("style");
    var jdActItem = jdAct[curLine];
    //正确选项
    var AnswerTrue = jdActItem["AnswerTrue"];
   //实际答题选项
    var ActAn = jdActItem["ActAn"];
    //是否正确，正确为"1",错误为"0"，未答为""
    var result = jdActItem["result"];
    if (result.length > 0) {//当错误时 加深正确的选择项

        if (AnswerTrue.indexOf("1") >= 0) {
            AnA = "<i class='spAnswerTrue'></i>"
            divAn1.style.color = "#1fae0c";
        }
        if (AnswerTrue.indexOf("2") >= 0) {
            AnB = "<i class='spAnswerTrue'></i>"
            divAn2.style.color = "#1fae0c";
        }
        if (AnswerTrue.indexOf("3") >= 0) {
            AnC = "<i class='spAnswerTrue'></i>"
            divAn3.style.color = "#1fae0c";
        }
        if (AnswerTrue.indexOf("4") >= 0) {
            AnD = "<i class='spAnswerTrue'></i>"
            divAn4.style.color = "#1fae0c";
        }
    }
    showSelBackcolor(ActAn);
    if (type == "1") {
        divAn3.style.display = "none";
        divAn4.style.display = "none";
        divAn1.innerHTML ="A:<span class='judgment'>、√</span>";
        divAn2.innerHTML ="B:<span class='judgment'>、×</span>";
    }
    else {
        divAn3.style.display = "block";
        divAn4.style.display = "block";
        An1 = replacekh(An1);
        An2 = replacekh(An2);
        An3 = replacekh(An3);
        An4 = replacekh(An4);
        divAn1.innerHTML = An1;
        divAn2.innerHTML = An2;
        divAn3.innerHTML = An3;
        divAn4.innerHTML = An4;
    }
    //图片和动画
    var divImg = document.getElementById("divImg");
    var imgFull = document.getElementById("imgFull");
    if (sinaimg.length > 3)
    {
        divImg.innerHTML = "<img src='" + sinaimg + "' onclick='showImgFull();' />";
        imgFull.src = sinaimg;
    }else {
        divImg.innerHTML = "";
    }
    //帮助
    var divhelp = document.getElementById("divhelp");
    explain = replacekh2span(explain)
    divhelp.innerHTML = explain;
    divhelp.style.textAlign = "left";
	//语音解说
    var divVoice = document.getElementById("divVoice");
    if (jq_mp3.length > 3)
    {
        if (isSkill == 0) {
            divVoice.innerHTML = "";
        }
        else {
            divVoice.innerHTML = "<audio id='audjq' src='" + jq_mp3 + "'></audio>";
        }
    }
    else {
        divVoice.innerHTML = "";
    }
    //语音读题
    var jques_mp3 = contentPath+jdr['mp32'];//视频地址
    var divQuesVoice = document.getElementById("divQuesVoice");
    if (jques_mp3.length > 3)
    {
        if (isSkill == 0) {
        	divQuesVoice.innerHTML = "";
        }
        else {
        	divQuesVoice.innerHTML = "<audio id='audjques' src='" + jques_mp3 + "' ></audio>";
        }
    }
    else {
    	divQuesVoice.innerHTML = "";
    }
	//图片解说
    var Skillimg = document.getElementById("Skillimg");
    var ImgObj = new Image();
    var url = jqimgpath;
    ImgObj.src = url;
    // 加载成功时
    ImgObj.onload = function () {
        if (ImgObj.width > 0 && ImgObj.height > 0) {
            Skillimg.innerHTML = "<img src='" + url + "'/>";
            Skillimg.style.textAlign = "center";
            voiceex = replacekh2span(voiceex)
            Skillimg.innerHTML += "<div style='text-align:left;'>"+ voiceex + "</div>";
        }
        else {
        	voiceex = replacekh2span(voiceex)
            Skillimg.innerHTML = voiceex;
            Skillimg.style.textAlign = "left";
        }
    }
    //加载失败时
    ImgObj.onerror = function ()
    {
    	voiceex = replacekh2span(voiceex)
        Skillimg.innerHTML = voiceex;
        Skillimg.style.textAlign = "left";
    }
}
function showKH()
{
    var jdr = jd["rec"][curLine];
    var line = jdr["line"];
    var id = jdr["id"];
    var type = jdr["type"];
    var typestr;
    switch (type) {
        case "1":
            typestr = "判断题";
            break;
        case "2":
            typestr = "单选题";
            break;
        case "3":
            typestr = "多选题";
            break;
        default:
            typestr = "不明";
            break;
    }
    var Question = jdr["question"];
    var An1 = jdr["an1"];
    var An2 = jdr["an2"];
    var An3 = jdr["an3"];
    var An4 = jdr["an4"];

    var divQuestion = document.getElementById("divQuestion");
    Question = replacekh2spanFlash(Question);
    divQuestion.innerHTML = (curLine + 1) + ". " + "<span class='radio'>" + typestr + "</span>" + Question;

    var divAn1 = document.getElementById("divAn1");
    var divAn2 = document.getElementById("divAn2");
    var divAn3 = document.getElementById("divAn3");
    var divAn4 = document.getElementById("divAn4");
    divAn1.style = "";
    divAn2.style = "";
    divAn3.style = "";
    divAn4.style = "";
    
    var jdActItem = jdAct[curLine];
    //正确选项
    var AnswerTrue = jdActItem["AnswerTrue"];
    //实际答题选项
    var ActAn = jdActItem["ActAn"];
    //是否正确，正确为"1",错误为"0"，未答为""
    var result = jdActItem["result"];
    if (result.length > 0) {
        AnA = "<i class='spAnswerFalse'></i>"
        AnB = "<i class='spAnswerFalse'></i>"
        AnC = "<i class='spAnswerFalse'></i>"
        AnD = "<i class='spAnswerFalse'></i>"

        if (AnswerTrue.indexOf("1") >= 0) {
            AnA = "<i class='spAnswerTrue'></i>"
            divAn1.style.color = "#1fae0c";
        }
        if (AnswerTrue.indexOf("2") >= 0) {
            AnB = "<i class='spAnswerTrue'></i>"
            divAn2.style.color = "#1fae0c";
        }
        if (AnswerTrue.indexOf("3") >= 0) {
            AnC = "<i class='spAnswerTrue'></i>"
            divAn3.style.color = "#1fae0c";
        }
        if (AnswerTrue.indexOf("4") >= 0) {
            AnD = "<i class='spAnswerTrue'></i>"
            divAn4.style.color = "#1fae0c";
        }
    }

    if (type == "1") {
        divAn3.style.display = "none";
        divAn4.style.display = "none";
        var AnswerTrue = jdActItem["AnswerTrue"];
        if (AnswerTrue == "1")
        {
            divAn1.innerHTML ="A:<span class='judgment'>、<span class='spanFlash'>√</span></span>";
            divAn2.innerHTML ="B:<span class='judgment'>、×</span>";
        }
        else
        {
            divAn1.innerHTML = "A:<span class='judgment'>、√</span>";
            divAn2.innerHTML = "B:<span class='judgment'>、<span class='spanFlash'>×</span></span>";
        }
    }
    else if (type == "3") {
        divAn3.style.display = "block";
        divAn4.style.display = "block";
        An1 = replacekh2spannoFlash(An1);
        An2 = replacekh2spannoFlash(An2);
        An3 = replacekh2spannoFlash(An3);
        An4 = replacekh2spannoFlash(An4);
        divAn1.innerHTML = An1;
        divAn2.innerHTML = An2;
        divAn3.innerHTML = An3;
        divAn4.innerHTML = An4;
    }
    else {
        divAn3.style.display = "block";
        divAn4.style.display = "block";

        An1 = replacekh2spanFlash(An1);
        An2 = replacekh2spanFlash(An2);
        An3 = replacekh2spanFlash(An3);
        An4 = replacekh2spanFlash(An4);

        divAn1.innerHTML = An1;
        divAn2.innerHTML = An2;
        divAn3.innerHTML = An3;
        divAn4.innerHTML = An4;
    }
    var ImgObj = new Image();
    
    var sinaimg = jdr["img"];//题目图片
	if (sinaimg.length > 3){
		sinaimg="/Data/img/"+jdr["img"];
	}
    ImgObj.src = sinaimg;
    // 加载成功时
    ImgObj.onload = function () {
        if (ImgObj.width > 0 && ImgObj.height > 0) {
            var divImg = document.getElementById("divImg");
            var imgFull = document.getElementById("imgFull");
            divImg.innerHTML = "<img src='" + sinaimg + "' onclick='showImgFull();' />";
            imgFull.src = sinaimg;           
        }
    }
}
//答题
function ansel(an)
{
	var jdActItem = jdAct[curLine];
    var AnswerTrue = jdActItem["AnswerTrue"];
    var type = jdActItem["type"];
    var ActAn = jdActItem["ActAn"];
    var result = jdActItem["result"];
    //已答过的题，不能再答
    if (result.length > 0)
    {
    	layer.msg("已答过此题");
        return;
    }
    if (type == "3") {
        //多选题
        var ma = new Array("0", "0", "0", "0");
        for (var i = 0; i < ActAn.length; i++) {
            var se = ActAn.substr(i, 1);
            ma[parseInt(se) - 1] = "1";
        }
        //取反
        if (ma[parseInt(an) - 1] == "0") {
            ma[parseInt(an) - 1] = "1";
        }
        else {
            ma[parseInt(an) - 1] = "0";
        }

        ActAn = "";
        for (var i = 0; i < ma.length; i++) {
            if (ma[i] == "1")
            {
                ActAn += (i + 1).toString();
            }
        }
        jdAct[curLine]["ActAn"] = ActAn;
        showSelBackcolor(ActAn);
    }
    else {
        jdActItem["ActAn"] = an;
        if (AnswerTrue == an)
        {
            jdActItem["result"] = "1";
        }
        else
        {
            jdActItem["result"] = "0";
        }
        //如果对就下一题，不对就停在本题
        if (AnswerTrue == an) {
            showQuestion();
            setTimeout("showD()", 1000);
        }
        else {
            showQuestion();
            playVoice();
        }
    }
}
function zimu(n)
{
  return String.fromCharCode(64 + parseInt(n));
}
//上一题
function showU() {
    curLine--;
    if (curLine < 0) {
        curLine = 0;
        return;
    }
    showQuestion();
    closequeoption();
}
//下一题
function showD() {
	
    var jdActItem = jdAct[curLine];
    var AnswerTrue = jdActItem["AnswerTrue"];
    var type = jdActItem["type"];
    var ActAn = jdActItem["ActAn"];
    var result = jdActItem["result"];
    if (type == "3" && result.length == 0 && ActAn.length > 0)
    {
        if (AnswerTrue == ActAn) {
            jdActItem["result"] = "1";
        }
        else {
            jdActItem["result"] = "0";
            //如果对就下一题，不对就停在本题
            showQuestion();
            return;
        }
    }

    curLine++;
    if (curLine > countLine - 1) {
        curLine = countLine - 1;
        //return;
    }
    showQuestion();
    closequeoption();
}
//下一题
function showD1() {
    var jdActItem = jdAct[curLine];
    var AnswerTrue = jdActItem["AnswerTrue"];
    var type = jdActItem["type"];
    var ActAn = jdActItem["ActAn"];
    var result = jdActItem["result"];
    if (type == "3" && result.length == 0 && ActAn.length > 0)
    {
        if (AnswerTrue == ActAn) {
            jdActItem["result"] = "1";
        }
        else {
            jdActItem["result"] = "0";
            //如果对就下一题，不对就停在本题
            showQuestion();
            return;
        }
    }

    curLine++;
    if (curLine > countLine - 1) {
        curLine = countLine - 1;
        //return;
    }
    showQuestion();
    document.getElementById("bg").style.display = "none";
    document.getElementById("popbox").style.display = "none";
    
}
//选项的背景色
function showSelBackcolor(ActAn)
{
    divAn1.style.backgroundColor = "";
    divAn2.style.backgroundColor = "";
    divAn3.style.backgroundColor = "";
    divAn4.style.backgroundColor = "";
    if (ActAn.indexOf("1") >= 0) {
        divAn1.style.backgroundColor = "#b3e3fb";
    }
    if (ActAn.indexOf("2") >= 0) {
        divAn2.style.backgroundColor = "#b3e3fb";
    }
    if (ActAn.indexOf("3") >= 0) {
        divAn3.style.backgroundColor = "#b3e3fb";
    }
    if (ActAn.indexOf("4") >= 0) {
        divAn4.style.backgroundColor = "#b3e3fb";
    }
}
//跳转到第i题
function showGo(i) {
    if (i == curLine)
    {
        return;
    }
    if (i > countLine - 1) {
        curLine = countLine - 1;
    }
    if (i < 0) {
        curLine = 0;
    }
    else {
        curLine = i;
    }
    showQuestion();
}
//不显示括号
function replacekh(str)
{
    str = str.replace(reg1, "");
    str = str.replace(reg2, "");
    return str;
}

//不显示括号，代替为span
function replacekh2span(str) {
    str = str.replace(reg1, "<span>");
    str = str.replace(reg2, "</span>");
    return str;
}

//不显示括号，代替为span(闪烁)
function replacekh2spanFlash(str) {
    str = str.replace(reg1, "<span class='spanFlash'>");
    str = str.replace(reg2, "</span>");
    return str;
}

//不显示括号，代替为span(不闪烁)
function replacekh2spannoFlash(str) {
    str = str.replace(reg1, "<span class='spannoFlash'>");
    str = str.replace(reg2, "</span>");
    return str;
}
//数字转百分比
function Percentage(num) {
    return (Math.round(num * 10000) / 100.00 + "%");// 小数点后两位百分比
}
//显示答题板
function showPanel()
{
    var ul_select = document.getElementById("ul_select");//面板选择器
    var selectright = document.getElementById("selectright");//正确显示
    var selecterror = document.getElementById("selecterror");//错误显示
    var selectrate = document.getElementById("selectrate");//正确率
    var r = 0;
    var e = 0;
    ul_select.innerHTML = "";
    var ul_selecthtml="";
    for (var i = 0; i < jdAct.length; i++) {
        var jdActItem = jdAct[i];
        //正确选项
        var AnswerTrue = jdActItem["AnswerTrue"];
        //实际答题选项
        var ActAn = jdActItem["ActAn"];
        //是否正确，正确为"1",错误为"0"，未答为""
        var result = jdActItem["result"];
        if (i == curLine) {
        	ul_selecthtml += "<li style='background-color:#a1a6fa;' onclick='showGo(" + i + ");'><span>" + (i + 1) + "</span></li>";
        }
        else if (result == "1") {
        	ul_selecthtml += "<li style='background-color:#036d46;color:white;' onclick='showGo(" + i + ");'><span>" + (i + 1) + "</span></li>";
        }
        else if (result == "0") {
        	ul_selecthtml += "<li style='background-color:#fbb7b7;' onclick='showGo(" + i + ");'><span>" + (i + 1) + "</span></li>";
        }
        else {
        	ul_selecthtml += "<li onclick='showGo(" + i + ");'><span>" + (i + 1) + "</span></li>";
        }
        if (result == "1") {
            r++;
        }
        else if (result == "0") {
            e++;
        }
    }
    ul_select.innerHTML = ul_selecthtml;
    selectright.innerHTML = r;
    selecterror.innerHTML = e;
    var p = r / (r + e);
    if (r + e == 0)
    {
        p = 0;
    }
    selectrate.innerHTML = Percentage(p);
}
//显示题目面板
function panelShow()
{
    var divPanel = document.getElementById("divPanel");
    divPanel.style.display = "block";
    var divMain = document.getElementById("divMain");
    divMain.style.display = "none";
}
//隐藏题目面板
function panelHide()
{
    var divPanel = document.getElementById("divPanel");
    divPanel.style.display = "none";
    var divMain = document.getElementById("divMain");
    divMain.style.display = "block";
}
//语音读题
function playQueOption(){
	var audios = document.getElementById("audjques");
	document.getElementById("bg").style.display = "none";
    document.getElementById("popbox").style.display = "none";
	var value=document.getElementById("playQueOptioninfo").innerHTML;
	if(value=='自动读题'){
		
		if (audios == null){
	    	
	    }else{
	    	document.getElementById("audjq").pause();
			document.getElementById("playQueOptioninfo").innerHTML="关闭读题";
	    	audios.load();
	    	audios.play();
	    	audios.addEventListener('ended', function () {
	    		document.getElementById("bg").style.display = "block";
	    	    document.getElementById("popbox").style.display = "block";
	    	    var video1 = document.getElementById("video1");
	    	    if (video1 != undefined) {
	    	        video1.style.display = "none";
	    	    }
	    		var ActAn= jdAct[curLine]['AnswerTrue'];
	    		jdAct[curLine]["ActAn"] = ActAn;
	    		jdAct[curLine]["result"] = 1;
	            showSelBackcolor(ActAn);//自动选题
	            setTimeout(function() {
	            	showD1();//下一题
	            	 document.getElementById("playQueOptioninfo").innerHTML="自动读题";
				}, 3000);	            
	        }, false);
	    }
	}else{
		document.getElementById("playQueOptioninfo").innerHTML="自动读题";
		if (audios == null){
	    }else{
	    	audios.pause();
	    }
		
	}
	showKH();
}
//关闭语音第题
function closequeoption(){
	document.getElementById("bg").style.display = "none";
    document.getElementById("popbox").style.display = "none";
	document.getElementById("playQueOptioninfo").innerHTML="自动读题";
	var audios = document.getElementById("audjques");
	if (audios == null){
    	
    }else{
    	audios.pause();
    }
}
//播放音频文件
function playVoice()
{
	document.getElementById("audjques").pause();
	document.getElementById("playQueOptioninfo").innerHTML="自动读题";
    var audio = document.getElementById("audjq");
    if (audio == null){
    }else{
    	audio.play();
    }
    document.getElementById("bg").style.display = "block";
    document.getElementById("popbox").style.display = "block";
    var video1 = document.getElementById("video1");
    if (video1 != undefined) {
        video1.style.display = "none";
    }
    showKH();
}

//重播
function playAgin() {
    var audio = document.getElementById("audjq");
    if (audio == null){
    }else{
    	audio.play();
    }
}
//关闭音频
function stopVoice() {
    document.getElementById("bg").style.display = "none";
    document.getElementById("popbox").style.display = "none";
    var video1 = document.getElementById("video1");
    if (video1 != undefined) {
        video1.style.display = "block";
    }
}
//显示帮助
function showHelp() {
    document.getElementById("bg2").style.display = "block";
    document.getElementById("popbox2").style.display = "block";
    var video1 = document.getElementById("video1");
    if (video1 != undefined) {
        video1.style.display = "none";
    }
    if (isSkill == 1) {
        showKH();
    }
}
//隐藏帮助
function hidHelp() {
    document.getElementById("bg2").style.display = "none";
    document.getElementById("popbox2").style.display = "none";
    var video1 = document.getElementById("video1");
    if (video1 != undefined) {
        video1.style.display = "block";
    }
}
//查看大图
function showImgFull() {
    document.getElementById("bg3").style.display = "block";
    document.getElementById("popbox3").style.display = "table";
}
//隐藏大图
function hidImgFull() {
    document.getElementById("bg3").style.display = "none";
    document.getElementById("popbox3").style.display = "none";
}


//交卷操作
function isEnd() {
    var r = 0;
    var e = 0;
    for (var i = 0; i < jdAct.length; i++) {
        var jdActItem = jdAct[i];
        //正确选项
        var AnswerTrue = jdActItem["AnswerTrue"];
        //实际答题选项
        var ActAn = jdActItem["ActAn"];
        //是否正确，正确为"1",错误为"0"，未答为""
        var result = jdActItem["result"];
        if (result == "1") {
            r++;
        }
        else if (result == "0") {
            e++;
        }
    }
    var p = r / (r + e);
    if (r + e == 0) {
        p = 0;
    }
    var score = r;
    if (countLine == 50)
    {
        score = score * 2;
    }
    var msg = "共" + countLine + "题\n正确" + r + "题\n错误" + e + "题\n未完成" + (countLine - r - e) + "题\n合计" + score + "分";
    layer.alert(msg);
    var errlist = [];
    for (var i = 0; i < jdAct.length; i++) {
        var result = jdAct[i]["result"];
        if (result == "1") {
            var id = jd["rec"][i]["id"];
            errlist.push(id);
        }
    }
    $.ajax({
        url: Examconfig.apiok,
        type: "post",
        data: {"errlist":errlist,"exam":Examconfig.exam,"examtype":Examconfig.examtype},
        async: false,
        datatype: "json",
        success: function (result) {
        	setTimeout(function(){
        		location.href = contentPath+result.url;
			},5000);
        },
        error: function () {
    	   layer.alert("上传历史错题夫败");
        }
    });
}